# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}4
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}4" "${MINGW_PACKAGE_PREFIX}-python-${_realname}4")
pkgver=4.19.25
pkgrel=1
pkgdesc="Tool to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL')
url="https://riverbankcomputing.com/software/sip"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(#"https://downloads.sourceforge.net/project/pyqt/${_realname}/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.gz"
        https://www.riverbankcomputing.com/static/Downloads/sip/${pkgver}/sip-${pkgver}.tar.gz
        "0001-mingw-python.patch"
        "0002-add-exe-in-sipconfig.patch")
sha256sums=('b39d93e937647807bac23579edbff25fe46d16213f708370072574ab1f1b4211'
            '2e3400c7467faca17b6efaae2e2420b0a253df9070b1ed0adcafcc10f9499a8d'
            '37b3c0b5da1731e1fb7044cd4af5e4ac81ee3f12f4b8a3a4584f3081441a4d31')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Apply patches
  patch -p1 -i "${srcdir}"/0001-mingw-python.patch
  patch -p1 -i "${srcdir}"/0002-add-exe-in-sipconfig.patch
}

build() {
  [[ -d python-${MINGW_CHOST} ]] && rm -rf python-${MINGW_CHOST}
  cp -r ${_realname}-${pkgver} python-${MINGW_CHOST}
  pushd python-${MINGW_CHOST}
    ${MINGW_PREFIX}/bin/python configure.py \
        --sip-module PyQt5.sip CFLAGS="${CFLAGS}" LFLAGS="${LDFLAGS}"
    make
  popd
}

package_sip4() {
  pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")

  cd "${srcdir}"/python-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C sipgen

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}4/LICENSE"
}

package_python-sip4() {
  pkgdesc="Python 3.x SIP bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-sip4" "${MINGW_PACKAGE_PREFIX}-python")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-sip4=${pkgver}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-sip")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-sip")

  cd python-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C siplib

  local _py3basever=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  sed -e "s|[\'\"]${MINGW_PREFIX}|sys.prefix \+ \'|g" -i sipconfig.py
  install -Dm644 sipconfig.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/sipconfig.py
  install -Dm644 sipdistutils.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/sipdistutils.py

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-sip4/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
